package bit.book.service;

import bit.book.dao.BookDao;
import bit.book.entity.BookInfo;
import bit.book.enums.BookStatus;
import bit.book.entity.PageRequest;
import bit.book.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BookService {

    @Autowired
    private BookMapper bookMapper;

    public List<BookInfo> getList(){
        BookDao bookDao = new BookDao();
        List<BookInfo> list = new ArrayList<>();
        list=bookDao.mocData();
        for(BookInfo bookInfo:list){
            if(bookInfo.getStatus()==1){
                bookInfo.setStatusCN("可借阅");
            }
            else{
                bookInfo.setStatusCN("不可借阅");
            }
        }
        return list;
    }

    public void addBook(BookInfo book) {
               bookMapper.addBook(book);


    }

    //SQL不支持在语句中运算，应该把page和size封装计算出offset，故此方法废弃
//    public List<BookInfo> getBookListByPage(Integer page, Integer size) {
//        List<BookInfo>bookInfoList= bookMapper.getBookListByPage(page,size);
//        for(BookInfo bookInfo:bookInfoList){
//            if(bookInfo.getStatus()==0){
//                bookInfo.setStatusCN("无效");
//            }
//            else if(bookInfo.getStatus()==1){
//                bookInfo.setStatusCN("可借阅");
//            }
//            else{
//                bookInfo.setStatusCN("不可借阅");
//            }
//        }
//        return bookInfoList;
//    }

    public Integer countBookByPage() {
        return bookMapper.countBookByPage();
    }

    public List<BookInfo> getBookListByPage(PageRequest pageRequest) {

        List<BookInfo>bookInfoList= bookMapper.getBookListByPage(pageRequest);
        for(BookInfo bookInfo:bookInfoList){
//            if(bookInfo.getStatus()==0){
//                bookInfo.setStatusCN("无效");
//            }
//            else if(bookInfo.getStatus()==1){
//                bookInfo.setStatusCN("可借阅");
//            }
//            else{
//                bookInfo.setStatusCN("不可借阅");
//            }
            bookInfo.setStatusCN(BookStatus.getBookStatusByCode(bookInfo.getStatus()));

        }
        return bookInfoList;
    }

    public BookInfo queryBookById(Integer bookId) {
        return bookMapper.queryBookById(bookId);
    }


    public void updateBook(BookInfo book) {
        bookMapper.updateBook(book);
    }


    public void deleteBook(BookInfo bookInfo) {
        bookMapper.updateBook(bookInfo);
    }

    public Integer batchDelete(List<Integer> ids) {
        return bookMapper.batchDelete(ids);
    }
}
